using System;
using System.Collections.Generic;
using System.Text;
using Mmht.DB;
using System.Data;

namespace Mmht.BT
{
    public class PageBT
    {
        public bool CreateOrUpdatePage(string type, string title, string content, int created_by)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            return pdb.CreateOrUpdatePage(type, title, content, created_by);
        }

        public bool AddPicture(string type, string picture)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            List<string> picList = new List<string>(this.GetPictures(type));
            if (picList.Contains(picture))
            {
                return true;
            }
            picList.Add(picture);
            string pics = string.Join("#", picList.ToArray());
            return pdb.UpdatePictures(type, pics);
        }

        public bool AddFile(string type, string file)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            pageRow = pdb.GetPageRow(type);
            List<string> fileList = new List<string>(this.GetFiles(type));
            if (fileList.Contains(file))
            {
                return true;
            }
            fileList.Add(file);
            string files = string.Join("#", fileList.ToArray());
            return pdb.UpdateFiles(type, files);
        }

        public bool DeletePicture(string type, string picture)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            pageRow = pdb.GetPageRow(type);
            List<string> picList = new List<string>(this.GetPictures(type));
            if (!picList.Contains(picture))
            {
                return true;
            }
            picList.Remove(picture);
            string pics = string.Join("#", picList.ToArray());
            return pdb.UpdatePictures(type, pics);
        }

        public bool DeleteFile(string type, string file)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            List<string> fileList = new List<string>(this.GetFiles(type));
            if (!fileList.Contains(file))
            {
                return true;
            }
            fileList.Remove(file);
            string files = string.Join("#", fileList.ToArray());
            return pdb.UpdateFiles(type, files);
        }

        public string GetTitle(string type)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            if (pageRow == null)
            {
                return "";
            }
            return pageRow["page_title"].ToString();
        }

        public string GetContent(string type)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            if (pageRow == null)
            {
                return "";
            }
            else return pageRow["page_content"].ToString();
        }

        public string[] GetPictures(string type)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            if (pageRow == null)
            {
                return new string[0];
            }
            else
            {
                string str_pictures = pageRow["pictures"].ToString().Trim();
                if (string.IsNullOrEmpty(str_pictures))
                {
                    return new string[0];
                }
                return str_pictures.Split('#');
            }
        }

        public string[] GetFiles(string type)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            if (pageRow == null)
            {
                return new string[0];
            }
            else
            {
                string str_files = pageRow["files"].ToString().Trim();
                if (string.IsNullOrEmpty(str_files))
                {
                    return new string[0];
                }
                return str_files.Split('#');
            }
        }

        public string GetAdminName(string type)
        {
            PageDB pdb = new PageDB();
            DataRow pageRow = pdb.GetPageRow(type);
            if (pageRow == null)
            {
                return "";
            }
            else
            {
                int id = Convert.ToInt32(pageRow["create_by"]);
                AdminBT ab = new AdminBT();
                return ab.FindbyId(id);
            }
        }

        public string[] ChangePicture(string[] pics)
        {
            string[] rst = new string[pics.Length];
            for (int i = 0; i < pics.Length; i++)
            {
                string pic = pics[i];
                rst[i] = string.Format("files/page_images/{0}", pic);
            }
            return rst;
        }
    }
}